package com.winit.openapi.wh.outbound.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.oms.spi.outbound.OutboundOrderTrackingService;
import com.winit.oms.spi.outbound.command.order.QueryOrderTrackingCommand;
import com.winit.oms.spi.outbound.vo.order.OutboundOrderTrackingVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
/**
 * 出库单轨迹
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * hui.gao	  1.0  		2016年5月19日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/outbound")
public class OutboundOrderTrackingController extends BaseController{
	
	@Resource
    private OutboundOrderTrackingService outboundOrderTrackingService;
	
	@RequestMapping(value = "/queryOrderTracking")
    public String queryOrderDetails(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        String orderNo = "";
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            orderNo = json.getString("orderNo");
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 查询订单轨迹
        QueryOrderTrackingCommand command  = new QueryOrderTrackingCommand();
        command.setOrderNo(orderNo);
        Page<OutboundOrderTrackingVo> page = outboundOrderTrackingService.getOrderTrackingByOrderNo(command);
        Map<String, Object> map = new HashMap<String, Object>();
        if (page != null) {
            List<OutboundOrderTrackingVo> dataList = page.getContent();
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (OutboundOrderTrackingVo vo : dataList) {
            	Map<String, Object> tempMap = new HashMap<String, Object>();
            	tempMap.put("trackingTime", vo.getTrackingTime());
            	tempMap.put("trackingMsg", vo.getTrackingMsg());
            	tempMap.put("trackingAddr", vo.getTrackingAddr());
            	tempMap.put("trackingCode", vo.getTrackingCode());
            	mapList.add(tempMap);
            }
            map.put("trackingList", mapList);
        }
        responseMsg.setData(map);
        return SUCCESS;
    }
}



    

    